package movenment;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CopyOfMainFrame2 implements ItemListener{
	JFrame frame;
	JPanel cards;
	JPanel card2;
	GamePanelTestThree bard;
	int highScore ;
	Highscore card3;
	CardLayout cl;
	final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    final static String HIGHSCORE = "Card with Highscore";
	public static void main(String args[]){
		new CopyOfMainFrame2().makeFrame();
	}
	
	/**
     * Create the Swing frame and its content.
     */
    private void makeFrame()
    {	
    	JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Snake!"); //Top-level Frame Named Snake!.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the applet when window is closed.
        
        frame.setSize(635, 460); //Sets the windows size to 640x480 , doesnt work with pack();
        frame.setMaximumSize(new Dimension(635, 460));
        frame.setMinimumSize(new Dimension(635, 460));
        frame.setPreferredSize(new Dimension(635, 460));
        frame.setResizable(false);
        // center the frame on screen
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
        
        
        
      //Create and set up the content pane.
        CopyOfMainFrame2 demo = new CopyOfMainFrame2();
        demo.addComponentToPane(frame.getContentPane());
        frame.pack(); 
        frame.setVisible(true); 
    }
  
    private void addComponentToPane(Container pane) {
    	//Pane is the contentpane of top-level frame.
    	//Put the JComboBox in a JPanel to get a nicer look.
        JPanel comboBoxPane = new JPanel(); //use FlowLayout
        String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL, HIGHSCORE };
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        
        //Create the "cards".
        ActionListener playAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) { play(); }
        };
        ActionListener scoreAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) { highScore(); }
        };
        ActionListener menuAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) { menu(); }
        };
        JPanel card1 = new Menu(playAction, scoreAction);
        //card1.add(new JButton("Button 1"));
        //card1.add(new JButton("Button 2"));
        //card1.add(new JButton("Button 3"));
        
        //card2 = new Bard();
        card2 = new GamePanelTestThree();
        //card2.add(new JTextField("TextField", 20));
        //card2.add(new Bard());
        card3 = new Highscore(menuAction);
        
        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        cards.add(card3, HIGHSCORE);
        //pane.add(comboBoxPane, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
		
	}

	protected void menu() {
		cl = (CardLayout)(cards.getLayout());
	    cl.show(cards, BUTTONPANEL);
	}

	protected void highScore() {
		cl = (CardLayout)(cards.getLayout());
	    cl.show(cards, HIGHSCORE);
	    card3.editable(false);
	}

	protected void play() {
		cl = (CardLayout)(cards.getLayout());
	    cl.show(cards, TEXTPANEL);
	    bard = (GamePanelTestThree) cards.getComponent(1);
	    
//	    bard = (Bard) cards.getComponent(1);
//	    bard.reset();
//	    bard.setDeathListener(new DeathListener(){
//	    	public void hasDied(){
//	    		highScore = bard.getHighscore();
//	    		card3.editable(true);
//	    		card3.setHighscore(highScore);
//	    		cl.show(cards, HIGHSCORE);
//	    	}
//	    });
	    bard.setDeathListener(new DeathListener(){
	    	public void hasDied(){
	    		highScore = bard.getHighScore();
	    		card3.editable(true);
	    		card3.setHighscore(highScore);
	    		cl.show(cards, HIGHSCORE);
	    	}
	    });
	    
	    
	    
	    bard.play();
	    bard.requestFocusInWindow();
	    //requestFocusInWindow();
	}

	@Override
	public void itemStateChanged(ItemEvent evt) {
		CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, (String)evt.getItem());
	}
    
}
